package org.example.leecode;
public class Leecode2{
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode sumList = new ListNode(0);
        ListNode current = sumList;
        int currentValue = 0;
        while (l1 != null || l2 != null || currentValue != 0 ){
            int val1 = l1 != null ? l1.val : 0;
            int val2 = l2 != null ? l2.val : 0;
            int sum = val1 + val2 + currentValue;
            int digit = sum / 10;
            int ge = sum % 10;
            current.next = new ListNode(ge);
            currentValue = digit;
            current = current.next;
            if(l1 != null) l1 = l1.next;
            if(l2 != null) l2 = l2.next;
        }
        return sumList.next;
    }

    class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
}